﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Collections;

namespace MCB.Net.Server
{
    public class TcpConnectionManager : IEnumerable<TcpConnection>
    {
        protected ConcurrentDictionary<string, TcpConnection> _connections =
            new ConcurrentDictionary<string, TcpConnection>();

        public TcpConnectionManager()
        {
        }

        public void Add(TcpConnection connection)
        {
            _connections.TryAdd(connection.Id, connection);
        }

        public void Remove(string connectionId)
        {
            TcpConnection connection;
            _connections.TryRemove(connectionId, out connection);
        }

        public void Remove(TcpConnection connection)
        {
            Remove(connection.Id);
        }

        public IEnumerator<TcpConnection> GetEnumerator()
        {
            return _connections.Values.GetEnumerator();
        }

        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public TcpConnection this[string connectionId]
        {
            get 
            {
                return _connections[connectionId];
            }
        }
    }
}
